package com.xinyue.ext.reader.core;

import org.dom4j.Element;

import com.xinyue.ext.Label;
import com.xinyue.ext.reader.ParamHolder;
import com.xinyue.ext.reader.bean.IdReader;
import com.xinyue.ext.reader.bean.OrderReader;
import com.xinyue.ext.reader.bean.PropertyReader;
import com.xinyue.ext.reader.bean.RelyReader;
import com.xinyue.ext.util.ListWrapper;

/**
 * 读取XML文件中的entity标签中的bean信息并返回封装对象
 */
public class BeanReader implements Label {
	//参数集
	private static ParamHolder holder;
	
	//私有构造器
	private BeanReader() {
	}
	//内部类
	private static class SingletonHolder{
        private static BeanReader instance = new BeanReader();  
    }
	/**
	 * 获得对象实例
	 */
	public static BeanReader getInstance(ParamHolder holder) {
		BeanReader.holder = holder;
		//bean对象的import文件路径集合
		BeanReader.holder.setBeanImpt(new ListWrapper());
		return SingletonHolder.instance;
	}
	
	/**
	 * 读取property信息并封装至BeanData对象
	 * 此方法传入entity标签，仅读取id及property标签，通过遍历匹配封装至BeanData对象
	 */
	public void read(Element entity) throws Exception {
		//id标签
		IdReader.getInstance(holder).read(entity);
		//property标签
		PropertyReader.getInstance(holder).read(entity);
		//order-by标签
		OrderReader.getInstance(holder).read(entity);
		//many和one标签
		RelyReader.getInstance(holder).read(entity);
		
		//set import
		holder.getEntity().setImports(holder.getBeanImpt().getList());
	}
}